﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using System.ComponentModel.Composition;
using System.Windows.Navigation;
using Cinch;
using MEFedMVVM.ViewModelLocator;
using System.Collections.ObjectModel;
using WebProject.Models;

namespace WebProject.ViewModels
{
    [ExportViewModel("MainPageViewModel")]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public class MainPageViewModel : ViewModelBase
    {

        public SimpleCommand<Object, EventToCommandArgs> Navigated { get; private set; }
        public SimpleCommand<Object, EventToCommandArgs> NavigationFailed { get; private set; }
        public ObservableCollection<NavItemInfo> NavItemsInfo { get; private set; }



        public MainPageViewModel()
        {
            NavItemsInfo = new ObservableCollection<NavItemInfo>();
            NavItemsInfo.Add(new NavItemInfo { ButtonContent = "home", PageUri = "/Home", SeperatorVisible = false, CurrentState = "ActiveLink" });
            NavItemsInfo.Add(new NavItemInfo { ButtonContent = "about", PageUri = "/About", SeperatorVisible = true, CurrentState = "InActiveLink" });
            NavItemsInfo.Add(new NavItemInfo { ButtonContent = "products", PageUri = "/Products", SeperatorVisible = true, CurrentState = "InActiveLink" });
            NavItemsInfo.Add(new NavItemInfo { ButtonContent = "map", PageUri = "/Map", SeperatorVisible = true, CurrentState = "InActiveLink" });
            // add any more pages here, note: seperator is only hidden on the first link

            // Setup the commands
            Navigated = new SimpleCommand<Object, EventToCommandArgs>(ExecuteNavigatedCommand);
            NavigationFailed = new SimpleCommand<Object, EventToCommandArgs>(ExecuteNavigationFailedCommand);
        }

        private void ExecuteNavigatedCommand(EventToCommandArgs args)
        {
            NavigationEventArgs navArgs = (NavigationEventArgs)args.EventArgs;
            foreach (var link in NavItemsInfo)
            {
                if (link.PageUri.ToString().Equals(navArgs.Uri.ToString()))
                {
                    link.CurrentState = "ActiveLink";
                }
                else
                {
                    link.CurrentState = "InActiveLink";
                }
            }
        }

        private void ExecuteNavigationFailedCommand(EventToCommandArgs args)
        {
            NavigationFailedEventArgs navArgs = (NavigationFailedEventArgs)args.EventArgs;
            navArgs.Handled = true;
            ChildWindow errorWin = new ErrorWindow(navArgs.Uri);
            errorWin.Show();
            // note: we could use the Cinch IChildWindowService for the error window
        }
    }
}
